﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class Them_ChinhSuaDonMuaHang : Form
    {
        public Them_ChinhSuaDonMuaHang()
        {
            InitializeComponent();
        }

        private void Them_ChinhSuaDonMuaHang_Load(object sender, EventArgs e)
        {
            Load_MaSo_NCU();
            Load_MaSo_LSP();
            Load_MaSo_SP(comboBoxDMH_LSP.SelectedValue.ToString());

            Load_DanhSachDMH();
            Load_DanhSachDMH_CT();
        }

        private void Load_DanhSachDMH()
        {
            Config file = new Config(Application.StartupPath + "//Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select MaSo_DMH, NgayMuaHang, DiaChi_NCU, XacNhan_DMH from DonMuaHang join NhaCungUng on DonMuaHang.MaSo_NCU = NhaCungUng.MaSo_NCU";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "DonMuaHang");
            DataTable dt = ds.Tables["DonMuaHang"];

            dataGridViewDanhSachDMH.DataSource = ds.Tables["DonMuaHang"].DefaultView;
        }

        private void Load_DanhSachDMH_CT()
        {
            int i = dataGridViewDanhSachDMH.SelectedRows[0].Index;
            string iddmh = dataGridViewDanhSachDMH.Rows[i].Cells["MaSo_DMH"].Value.ToString();

            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select MaSo_DMH, SanPham.MaSo_SP, Ten_SP, Ten_LSP, SoLuong, GiaMua" + " from DonMuaHang_CT join SanPham on DonMuaHang_CT.MaSo_SP = SanPham.MaSo_SP join LoaiSanPham on SanPham.MaSo_LSP = LoaiSanPham.MaSo_LSP" + " where MaSo_DMH = @iddmh";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@iddmh", iddmh);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "DonMuaHang_CT");
            DataTable dt = ds.Tables["DonMuaHang_CT"];

            dataGridViewDanhSachDMH_CT.AutoGenerateColumns = false;
            dataGridViewDanhSachDMH_CT.DataSource = ds.Tables["DonMuaHang_CT"].DefaultView;
        }

        private void Load_MaSo_NCU()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionString = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from NhaCungUng";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "NhaCungUng");
            DataTable dt = ds.Tables["NhaCungUng"];

            comboBoxDiaChi_NCU.DisplayMember = "DiaChi_NCU";
            comboBoxDiaChi_NCU.ValueMember = "MaSo_NCU";
            comboBoxDiaChi_NCU.DataSource = ds.Tables["NhaCungUng"].DefaultView;
        }

        private void Load_MaSo_SP(string loai)
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from SanPham where MaSo_LSP = @loai";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@loai", loai);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "SanPham");
            DataTable dt = ds.Tables["SanPham"];

            comboBoxDMH_Ten_SP.DisplayMember = "Ten_SP";
            comboBoxDMH_Ten_SP.ValueMember = "MaSo_SP";
            comboBoxDMH_Ten_SP.DataSource = ds.Tables["SanPham"];
        }

        private void Load_MaSo_LSP()
        {
            Config file = new Config(Application.StartupPath + "//Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from LoaiSanPham";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "LoaiSanPham");
            DataTable dt = ds.Tables["LoaiSanPham"];

            comboBoxDMH_LSP.DisplayMember = "Ten_LSP";
            comboBoxDMH_LSP.ValueMember = "MaSo_LSP";
            comboBoxDMH_LSP.DataSource = ds.Tables["LoaiSanPham"];
        }

        private void comboBoxDMH_LSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_MaSo_SP(comboBoxDMH_LSP.SelectedValue.ToString());
        }

        private void Load_1()
        {
            int i = dataGridViewDanhSachDMH.SelectedRows[0].Index;
            string iddmh = dataGridViewDanhSachDMH.Rows[i].Cells["MaSo_DMH"].Value.ToString();
            string date = dataGridViewDanhSachDMH.Rows[i].Cells["NgayMuaHang"].Value.ToString();

            Config file = new Config(Application.StartupPath + "//Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select * from DonMuaHang where MaSo_DMH = @iddmh ";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@iddmh", iddmh);
            //cmd.Parameters.AddWithValue("@ngay", date);

            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                dateTimePickerNgayMuaHang.Value = (DateTime)reader["NgayMuaHang"];
                textBoxXacNhan_DMH.Text = reader["XacNhan_DMH"].ToString();
                comboBoxDiaChi_NCU.SelectedValue = reader["MaSo_NCU"].ToString();
            }
            reader.Close();
            conn.Close();
        }

        private void Load_2()
        {
            int i = dataGridViewDanhSachDMH_CT.SelectedRows[0].Index;
            string idsp = dataGridViewDanhSachDMH_CT.Rows[i].Cells["MaSo_SP"].Value.ToString();

            Config file = new Config(Application.StartupPath + "//Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "select MaSo_DMH, SanPham.MaSo_SP, LoaiSanPham.MaSo_LSP, SoLuong, GiaMua" +
                           " from DonMuaHang_CT join SanPham on DonMuaHang_CT.MaSo_SP = SanPham.MaSo_SP join LoaiSanPham on SanPham.MaSo_LSP = LoaiSanPham.MaSo_LSP" +
                           " where DonMuaHang_CT.MaSo_SP = @idsp";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@idsp", idsp);

            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                comboBoxDMH_LSP.SelectedValue = reader["MaSo_LSP"].ToString();
                comboBoxDMH_Ten_SP.SelectedValue = reader["MaSo_SP"].ToString();
                textBoxSoLuong.Text = reader["SoLuong"].ToString();
                textBoxGiaMua.Text = reader["GiaMua"].ToString();
            }
            reader.Close();
            conn.Close();
        }

        private void dataGridViewDanhSachDMH_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Load_1();
            Load_DanhSachDMH_CT();
            textBoxSoLuong.Text = "";
            textBoxGiaMua.Text = "";
        }

        private void dataGridViewDanhSachDMH_CT_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Load_2();
        }

        private void butttonThemDMH_Click(object sender, EventArgs e)
        {
            ThemDonMuaHang f = new ThemDonMuaHang();
            if (f.ShowDialog() == DialogResult.OK)
            {
                Load_DanhSachDMH();
                Load_DanhSachDMH_CT();
            }
        }

        private void updateDMH()
        {
            int i = dataGridViewDanhSachDMH.SelectedRows[0].Index;
            string id = dataGridViewDanhSachDMH.Rows[i].Cells["MaSo_DMH"].Value.ToString();

            Config file = new Config(Application.StartupPath + "//Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "update DonMuaHang set MaSo_NCU = @MaSo_NCU, XacNhan_DMH = @xndmh where MaSo_DMH = @MaSo_DMH ";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@MaSo_DMH", id);
            cmd.Parameters.AddWithValue("@MaSo_NCU", comboBoxDiaChi_NCU.SelectedValue);
            cmd.Parameters.AddWithValue("@xndmh", textBoxXacNhan_DMH.Text);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            Load_DanhSachDMH();
        }

        private void buttonCapNhatDMH_Click(object sender, EventArgs e)
        {
            ThongBaoThayDoi f = new ThongBaoThayDoi();
            if (f.ShowDialog() == DialogResult.OK)
            {
                updateDMH();
                f.Close();
                Load_DanhSachDMH();
            }
            else
                f.Close();
        }

        private void buttonXoaDMH_Click(object sender, EventArgs e)
        {
            //if ((MessageBox.Show("Bạn có muốn xóa dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
            ThongBaoXoa f = new ThongBaoXoa();
            if (f.ShowDialog() == DialogResult.OK)
            {
                int i = dataGridViewDanhSachDMH.SelectedRows[0].Index;
                string iddmh = dataGridViewDanhSachDMH.Rows[i].Cells["MaSo_DMH"].Value.ToString();

                //Xóa tất cả dữ liệu sản phẩm của hợp đồng trong bảng dataGridViewDanhSachDMH_CT
                Config file = new Config(Application.StartupPath + "//Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "delete from DonMuaHang_CT where MaSo_DMH = @iddmh";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@iddmh", iddmh);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                //Xóa dữ liệu trong bảng dataGridViewDanhSachDMH
                cmd.Parameters.Clear();
                cmd.CommandText = "delete from DonMuaHang where MaSo_DMH = @iddmh";
                cmd.Parameters.AddWithValue("@iddmh", iddmh);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                Load_DanhSachDMH_CT();
                dataGridViewDanhSachDMH.Rows.RemoveAt(i);
                dateTimePickerNgayMuaHang.Text = "";
            }
            else
            {
                f.Close();
            }
        }

        private void buttonThemSP_Click(object sender, EventArgs e)
        {
            int i = dataGridViewDanhSachDMH.SelectedRows[0].Index;
            string id = dataGridViewDanhSachDMH.Rows[i].Cells["MaSo_DMH"].Value.ToString();
            string date = dataGridViewDanhSachDMH.Rows[i].Cells["NgayMuaHang"].Value.ToString();

            ThemSanPhamDMH_CT f = new ThemSanPhamDMH_CT(id, date);
            if (f.ShowDialog() == DialogResult.OK)
            {
                Load_DanhSachDMH_CT();
            }
        }

        private void updateSP()
        {
            int i = dataGridViewDanhSachDMH.SelectedRows[0].Index;
            string iddmh = dataGridViewDanhSachDMH.Rows[i].Cells["MaSo_DMH"].Value.ToString();
            int j = dataGridViewDanhSachDMH_CT.SelectedRows[0].Index;
            string idsp = dataGridViewDanhSachDMH_CT.Rows[j].Cells["MaSo_SP"].Value.ToString();

            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionstring = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionstring);
            string sql = "update DonMuaHang_CT set SoLuong = @sl, GiaMua = @gm where MaSo_DMH = @iddmh and MaSo_SP = @idsp";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@iddmh", iddmh);
            cmd.Parameters.AddWithValue("@idsp", idsp);
            cmd.Parameters.AddWithValue("@sl", textBoxSoLuong.Text);
            cmd.Parameters.AddWithValue("@gm", textBoxGiaMua.Text);

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            Load_DanhSachDMH_CT();
        }

        private void buttonCapNhatSP_Click(object sender, EventArgs e)
        {
            ThongBaoThayDoi f = new ThongBaoThayDoi();
            if (f.ShowDialog() == DialogResult.OK)
            {
                updateSP();
                f.Close();
                Load_DanhSachDMH_CT();
            }
            else
                f.Close();
        }

        private void buttonXoaSP_Click(object sender, EventArgs e)
        {
            //if ((MessageBox.Show("Bạn có muốn xóa dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
            ThongBaoXoa f = new ThongBaoXoa();
            if (f.ShowDialog() == DialogResult.OK)
            {
                int i = dataGridViewDanhSachDMH.SelectedRows[0].Index;
                string id = dataGridViewDanhSachDMH.Rows[i].Cells["MaSo_DMH"].Value.ToString();
                int j = dataGridViewDanhSachDMH_CT.SelectedRows[0].Index;
                string idsp = dataGridViewDanhSachDMH_CT.Rows[j].Cells["MaSo_SP"].Value.ToString();

                Config file = new Config(Application.StartupPath + "//Config.ini");
                string connectionstring = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionstring);
                string sql = "delete from DonMuaHang_CT where MaSo_DMH = @MaSo_DMH and MaSo_SP = @MaSo_SP";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@MaSo_DMH", id);
                cmd.Parameters.AddWithValue("@MaSo_SP", idsp);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                dataGridViewDanhSachDMH_CT.Rows.RemoveAt(j);

                textBoxSoLuong.Text = "";
                textBoxGiaMua.Text = "";
            }
            else
            {
                f.Close();
            }
        }
    }
}
